package com.wkbb.sports.controller;

import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.wkbb.common.base.BaseController;
import com.wkbb.common.constants.ErrorCode;
import com.wkbb.common.constants.VersionConstant;
import com.wkbb.common.dto.ResultDTO;
import com.wkbb.common.dto.basic.UserDto;
import com.wkbb.sports.dto.*;
import com.wkbb.sports.exception.MyException;
import com.wkbb.sports.exception.ResultEnum;
import com.wkbb.sports.feign.BasicFeignService;
import com.wkbb.sports.model.HlifePhysical;
import com.wkbb.sports.model.HlifeWorkStudentHistory;
import com.wkbb.sports.service.WorkService;
import com.wkbb.sports.utils.RedisOfRank;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lbh
 * @date 2021年03月29日 17:58:34
 * @description:   作业控制类
 */
@RestController
@Api(tags = "作业接口")
@RequestMapping("/sports/{apiVersion}/work")
@Slf4j
public class WorkController extends BaseController {
    @Autowired
    private BasicFeignService basicFeignService;

    @Autowired
    private WorkService workService;

//    @Autowired
//    private TestRedis testRedis;

    @Autowired
    private RedisOfRank redisOfRank;

    //===========================================平板端=============【begin】===============================================================

    @ApiOperation(value = "布置作业/新增作业")
    @RequestMapping(value = "insertWork", method = RequestMethod.POST)
    public ResultDTO insertWork(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @RequestBody HifeWorkDto hifeWorkDto){
            //PC和平板端
            UserDto userDto = this.getUserByLogin();
            if (null == userDto) {
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
            }
            return workService.insertWork(hifeWorkDto, userDto);
           /* try {
                return workService.insertWork(hifeWorkDto, userDto);
            } catch (Exception e) {
                log.error("***********WorkController.insertWork******", e);
                return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
            }*/
    }

    @ApiOperation(value = "学生提交作业")
    @RequestMapping(value = "insertWorkStudentHistory", method = RequestMethod.POST)
    public ResultDTO insertWorkStudentHistory(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @RequestBody HlifeWorkStudentHistory hlifeWorkStudentHistory){
        //PC和平板端
        //UserDto userDto = this.getUserByUnion();
        UserDto userDto = this.getRouteUserByUnion();
        if (null == userDto) {
            return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
        }
        /*UserDto userDto = new UserDto();
        userDto.setName("小成成");
        userDto.setHeadImgUrl("https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132");
*/
        return workService.insertWorkStudentHistory(hlifeWorkStudentHistory, userDto);
        /*try {
            return workService.insertWorkStudentHistory(hlifeWorkStudentHistory, userDto);
        } catch (Exception e) {
            log.error("***********WorkController.insertWorkStudentHistory******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }*/
    }

    @ApiOperation(value = "查询当前作业的学生的排名")
    @RequestMapping(value = "getStudentPositionOfWork", method = RequestMethod.GET)
    public ResultDTO<List<Map<String, Object>>> getStudentPositionOfWork(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "作业ID", required = true) @RequestParam(name = "workId", required = true) String workId){
        return workService.getStudentPositionOfWork(workId);
    }

    @ApiOperation(value = "查询当前作业的学生的排名【前三名或者含自己的四名】")
    @RequestMapping(value = "getStudentPositionOfWorkThird", method = RequestMethod.GET)
    public ResultDTO<List<Map<String, Object>>> getStudentPositionOfWorkThird(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "作业ID", required = true) @RequestParam(name = "workId", required = true) String workId,
            @ApiParam(value = "学生ID", required = true) @RequestParam(name = "studentId", required = true) String studentId){
        return workService.getStudentPositionOfWorkThird(workId,studentId);
    }

    @ApiOperation(value = "查询前三名或者含自己的前面的两名")
    @RequestMapping(value = "getStudentByPosition", method = RequestMethod.GET)
    public ResultDTO<List<Map<String, Object>>> getStudentByPosition(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "作业ID", required = true) @RequestParam(name = "workId", required = true) String workId,
            @ApiParam(value = "标识(0查询前三名或者含自己的前面的两名、1查询自己排名前的所有【默认】)", required = false,defaultValue = "1") @RequestParam(name = "mark", required = false,defaultValue = "1") String mark,
            @ApiParam(value = "学生ID", required = true) @RequestParam(name = "studentId", required = true) String studentId){
        return workService.getStudentByPosition(workId,studentId,mark);
    }

    @ApiOperation(value = "查询项目下拉列表的值")
    @RequestMapping(value = "getItemValue", method = RequestMethod.GET)
    public ResultDTO<List<HlifePhysical>> getItemValue(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "学校ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId,
            @ApiParam(value = "项目类型(1跳绳 2跑步)", required = true) @RequestParam(name = "projectType", required = true) Long projectType){
        return workService.getItemValue(orgId, projectType);
        /*try {
            return workService.getItemValue(orgId, projectType);
        } catch (Exception e) {
            log.error("***********WorkController.getItemValue******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }*/
    }

    @ApiOperation(value = "最近作业列表")
    @RequestMapping(value = "getWorkListByTeacherId", method = RequestMethod.GET)
    public ResultDTO<PageInfo<WorkDto>> getWorkListByTeacherId(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "学校ID", required = false) @RequestParam(name = "orgId", required = false) Integer orgId,
            @ApiParam(value = "教师ID", required = true) @RequestParam(name = "teacherId", required = true) Integer teacherId,
            @ApiParam(value = "项目id", required = true) @RequestParam(name = "projectId", required = false) Integer projectId,
            @ApiParam(value = "班级id", required = true) @RequestParam(name = "classId", required = false) Integer classId,
            @ApiParam(value = "时间范围(示例值为：2021-04-03或者2021-04-03-2021-04-05)", required = false) @RequestParam(name = "time", required = false) String time,
            @ApiParam(value = "分页数", defaultValue = "1", required = false) @RequestParam(name = "pageNo", defaultValue = "1", required = false) int pageNo,
            @ApiParam(value = "分页大小", defaultValue = "20", required = false) @RequestParam(name = "pageSize", defaultValue = "20", required = false) int pageSize){
        return workService.getWorkListByTeacherId(teacherId, projectId, classId, time,orgId,pageNo,pageSize);
        /*try {
            return workService.getWorkListByTeacherId(teacherId, projectId, classId, time);
        } catch (Exception e) {
            log.error("***********WorkController.getWorkListByTeacherId******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }*/
    }

    @ApiOperation(value = "作业详情")
    @RequestMapping(value = "getWorkDetail", method = RequestMethod.GET)
    public ResultDTO<Map<String,Object>> getWorkDetail(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "作业ID", required = true) @RequestParam(name = "workId", required = true) String workId){
        return workService.getWorkDetail(workId);
    }

    @ApiOperation(value = "获取作业指定日期达标与未达标列表")
    @RequestMapping(value = "getWorkStudentByDate", method = RequestMethod.GET)
    public ResultDTO<Map<String,Object>> getWorkStudentByDay(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "作业ID", required = true) @RequestParam(name = "workId", required = true) String workId,
            @ApiParam(value = "指定日期 例：2022-11-09", required = true) @RequestParam(name = "date", required = true) String date){
        return workService.getWorkStudentByDate(workId,date);
    }

    /*@ApiOperation(value = "删除作业及其相关联信息")
    @RequestMapping(value = "deleteWorkInfo", method = RequestMethod.POST)
    public ResultDTO deleteWorkInfo(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "作业ID", required = true) @RequestParam(name = "workId", required = true) String workId){
        return workService.deleteWorkInfo(workId);
        *//*try {
            return workService.deleteWorkInfo(workId);
        } catch (Exception e) {
            log.error("***********WorkController.deleteWorkInfo******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }*//*
    }*/

    @ApiOperation(value = "【结束/删除/立即开始】作业及其相关联信息")
    @RequestMapping(value = "endAndStartWorkInfo", method = RequestMethod.POST)
    public ResultDTO endAndStartWorkInfo(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "作业ID", required = true) @RequestParam(name = "workId", required = true) String workId,
            @ApiParam(value = "作业状态(0未开始 1进行中2已结束3已取消)", required = true) @RequestParam(name = "status", required = true) String status){
        return workService.endAndStartWorkInfo(workId,status);
        /*try {
            return workService.endWorkInfo(workId);
        } catch (Exception e) {
            log.error("***********WorkController.endWorkInfo******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }*/
    }

    @ApiOperation(value = "个人作业记录【时长模式】【平板端】")
    @RequestMapping(value = "getStudentPersonalInfoDetail", method = RequestMethod.GET)
    public ResultDTO<List<WorkStudentHistoryDto>> getStudentPersonalInfoDetail(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "主键ID", required = true) @RequestParam(name = "studentHistoryId", required = true) Long studentHistoryId){
        return workService.getStudentPersonalInfoDetail(studentHistoryId);
        /*try {
            return workService.getStudentPersonalInfoDetail(workId,studentId,studentHistoryId);
        } catch (Exception e) {
            log.error("***********WorkController.getStudentPersonalInfoDetail******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }*/
    }

    @ApiOperation(value = "定时改变作业状态")
    @RequestMapping(value = "updateWorkStatus", method = RequestMethod.GET)
    public void updateWorkStatus(){
        workService.updateWorkStatus();
    }

    @ApiOperation(value = "教师催促作业")
    @RequestMapping(value = "alert", method = RequestMethod.POST)
    public ResultDTO alert( @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
                                  @ApiParam(value = "作业ID", required = true) @RequestParam(name = "workId", required = true) String workId){
        return workService.alert(workId);
        /*try {
            return workService.alert(workId);
        } catch (Exception e) {
            log.error("***********WorkController.alert******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }*/
    }



//===========================================平板端=============【end】===============================================================


//===========================================小程序端=============【begin】===============================================================

    @ApiOperation(value = "学生在小程序端查询属于自己的作业列表/作业详情")
    @RequestMapping(value = "getWorkListByStudentId", method = RequestMethod.GET)
    public ResultDTO<PageInfo<WorkDto>> getWorkListByStudentId(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "学生ID", required = true) @RequestParam(name = "studentId", required = true) String studentId,
            @ApiParam(value = "作业ID", required = false) @RequestParam(name = "workId", required = false) String workId,
            @ApiParam(value = "达标状态(0未达标，1已达标)", required = false) @RequestParam(name = "reached", required = false) Integer reached,
            @ApiParam(value = "分页数", defaultValue = "1", required = false) @RequestParam(name = "pageNo", defaultValue = "1", required = false) int pageNo,
            @ApiParam(value = "分页大小", defaultValue = "20", required = false) @RequestParam(name = "pageSize", defaultValue = "20", required = false) int pageSize){
        //小程序端
        /*UserDto userDto = this.getUserByUnion();
        if (null == userDto) {
            return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
        }*/
        return workService.getWorkListByStudentId(studentId,workId,reached,pageNo,pageSize);
        /*try {
            return workService.getWorkListByStudentId(studentId,workId);
        } catch (Exception e) {
            log.error("***********WorkController.getWorkListByStudentId******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }*/
    }


    @ApiOperation(value = "个人作业记录【时长模式】【小程序端】")
    @RequestMapping(value = "getStudentWorkHistory", method = RequestMethod.GET)
    public ResultDTO<PageInfo<WorkStudentHistoryDto> > getStudentWorkHistory(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "主键ID", required = true) @RequestParam(name = "studentHistoryId", required = true) Long studentHistoryId,
            @ApiParam(value = "分页数", defaultValue = "1", required = false) @RequestParam(name = "pageNo", defaultValue = "1", required = false) int pageNo,
            @ApiParam(value = "分页大小", defaultValue = "20", required = false) @RequestParam(name = "pageSize", defaultValue = "20", required = false) int pageSize){
        return workService.getStudentWorkHistory(studentHistoryId,pageNo,pageSize);
        /*try {
            return workService.getStudentPersonalInfoDetail(workId,studentId,studentHistoryId);
        } catch (Exception e) {
            log.error("***********WorkController.getStudentPersonalInfoDetail******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }*/
    }

    @ApiOperation(value = "获取某个作业已达标/未达标的学生列表")
    @RequestMapping(value = "getUnAndReachStudentList", method = RequestMethod.GET)
    public ResultDTO<PageInfo<WorkStuDto>> getUnAndReachStudentList(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "作业ID", required = true) @RequestParam(name = "workId", required = true) String workId,
            @ApiParam(value = "达标状态(0未达标，1已达标)", required = false,defaultValue = "1") @RequestParam(name = "reached", required = false,defaultValue = "1") Integer reached,
            @ApiParam(value = "分页数", defaultValue = "1", required = false) @RequestParam(name = "pageNo", defaultValue = "1", required = false) int pageNo,
            @ApiParam(value = "分页大小", defaultValue = "20", required = false) @RequestParam(name = "pageSize", defaultValue = "20", required = false) int pageSize){
        return workService.getUnAndReachStudentList(workId,reached, pageNo,pageSize);
    }

//===========================================小程序端=============【end】===============================================================

    //@ApiOperation(value = "testRedis")
    @RequestMapping(value = "testRedis", method = RequestMethod.GET)
    public ResultDTO<PageInfo<Map<String,Object>>>  testRedis(
            @ApiParam(value = "类型", required = true, defaultValue = "2") @RequestParam(value = "type", required = true) String type){
        log.info("===========type============："+type);
       /* if(StringUtils.equals(type,"1")){
            testRedis.batchAdd();
        }else if(StringUtils.equals(type,"2")){
            testRedis.list();
        }else if(StringUtils.equals(type,"3")){
            testRedis.add();
        }else if(StringUtils.equals(type,"4")){
            testRedis.find();
        }else if(StringUtils.equals(type,"5")){
            testRedis.count();
        }else if(StringUtils.equals(type,"6")){
            testRedis.zCard();
        }else if(StringUtils.equals(type,"7")){
            testRedis.incrementScore();
        }else{
            log.info("===========无此type============");
        }*/

        if(StringUtils.equals(type,"1")){
//            redisOfRank.addMap();
        }else if(StringUtils.equals(type,"2")){
//            redisOfRank.listMap();
        }else if(StringUtils.equals(type,"3")){
//            redisOfRank.findRankNumForMap(type);
        }else{
            log.info("===========无此type============");
        }

        return new ResultDTO<>();
    }

}